package cn.iocoder.yudao.module.shop.controller.admin.shopSettleApply;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.shopSettleApply.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.shopSettleApply.ShopSettleApplyDO;
import cn.iocoder.yudao.module.shop.service.shopSettleApply.ShopSettleApplyService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 商户入驻申请")
@RestController
@RequestMapping("/dc/shop-settle-apply")
@Validated
public class ShopSettleApplyController {

    @Resource
    private ShopSettleApplyService shopSettleApplyService;

    @PostMapping("/create")
    @Operation(summary = "创建商户入驻申请")
    @PreAuthorize("@ss.hasPermission('dc:shop-settle-apply:create')")
    public CommonResult<Long> createShopSettleApply(@Valid @RequestBody ShopSettleApplySaveReqVO createReqVO) {
        return success(shopSettleApplyService.createShopSettleApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商户入驻申请")
    @PreAuthorize("@ss.hasPermission('dc:shop-settle-apply:update')")
    public CommonResult<Boolean> updateShopSettleApply(@Valid @RequestBody ShopSettleApplySaveReqVO updateReqVO) {
        shopSettleApplyService.updateShopSettleApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商户入驻申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dc:shop-settle-apply:delete')")
    public CommonResult<Boolean> deleteShopSettleApply(@RequestParam("id") Long id) {
        shopSettleApplyService.deleteShopSettleApply(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商户入驻申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dc:shop-settle-apply:query')")
    public CommonResult<ShopSettleApplyRespVO> getShopSettleApply(@RequestParam("id") Long id) {
        ShopSettleApplyDO shopSettleApply = shopSettleApplyService.getShopSettleApply(id);
        return success(BeanUtils.toBean(shopSettleApply, ShopSettleApplyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商户入驻申请分页")
    @PreAuthorize("@ss.hasPermission('dc:shop-settle-apply:query')")
    public CommonResult<PageResult<ShopSettleApplyRespVO>> getShopSettleApplyPage(@Valid ShopSettleApplyPageReqVO pageReqVO) {
        PageResult<ShopSettleApplyDO> pageResult = shopSettleApplyService.getShopSettleApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ShopSettleApplyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商户入驻申请 Excel")
    @PreAuthorize("@ss.hasPermission('dc:shop-settle-apply:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportShopSettleApplyExcel(@Valid ShopSettleApplyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ShopSettleApplyDO> list = shopSettleApplyService.getShopSettleApplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商户入驻申请.xls", "数据", ShopSettleApplyRespVO.class,
                        BeanUtils.toBean(list, ShopSettleApplyRespVO.class));
    }

}